'use strict'

const { DataTypes, Model, Op } = require('../../../azeroth').sequelize;
class Flaw_1 extends Model { }

const initFlawData_1 = (sequelize_aim) => {
    Flaw_1.init(
        {
            uid: {
                autoIncrement: true,
                type: DataTypes.INTEGER,
                allowNull: false,
                primaryKey: true
            },
            jobId: {
                type: DataTypes.INTEGER,
                allowNull: false,
                defaultValue: 0,
                primaryKey: true
            },
            doffId: {
                type: DataTypes.INTEGER,
                allowNull: true
            },
            laneId: {
                type: DataTypes.INTEGER,
                allowNull: true
            },
            flawClassType: {
                type: DataTypes.STRING(128),
                allowNull: true
            },
            dCD: {
                type: DataTypes.DECIMAL(16, 3),
                allowNull: true
            },
            dMD: {
                type: DataTypes.DECIMAL(16, 3),
                allowNull: true
            },
            dWidth: {
                type: DataTypes.DECIMAL(16, 3),
                allowNull: true
            },
            dLength: {
                type: DataTypes.DECIMAL(16, 3),
                allowNull: true
            },
            dArea: {
                type: DataTypes.DECIMAL(16, 3),
                allowNull: true
            },
            dDiameter: {
                type: DataTypes.DECIMAL(16, 3),
                allowNull: true
            },
            dRectMl: {
                type: DataTypes.STRING(128),
                allowNull: true
            },
            dShape: {
                type: DataTypes.INTEGER,
                allowNull: true
            },
            cameraId: {
                type: DataTypes.INTEGER,
                allowNull: true
            },
            createTime: {
                type: DataTypes.BIGINT,
                allowNull: true
            },
            frameNum: {
                type: DataTypes.INTEGER,
                allowNull: true
            },
            station: {
                type: DataTypes.STRING(128),
                allowNull: true
            },
            content: {
                type: DataTypes.STRING(256),
                allowNull: true
            },
            exField: {
                type: DataTypes.TEXT,
                allowNull: true
            }
        },
        {
            freezeTableName: true,
            sequelize: sequelize_aim,
            modelName: 'flaw',
            timestamps: false
        }
    )
}

Flaw_1.getMaxUid = async (sequelize_aim) => {
    let res = await sequelize_aim.query(`select max(uid) as uid from flaw`, { type: sequelize_aim.QueryTypes.SELECT });
    if (res.length) {
        return res[0].uid;
    } else {
        return 0;
    }
}

Flaw_1.upload = async (data) => {
    let res = await Flaw_1.bulkCreate(data, {
        updateOnDuplicate: ['uid', 'jobId', 'doffId', 'flawClassType', 'dCD', 'dMD', 'dWidth', 'dLength', 'dArea', 'dDiameter', 'dShape', 'cameraId', 'createTime', 'station', 'content', 'exField']
    });
    return res;
}

module.exports = {
    Flaw_1,
    initFlawData_1
};